package com.dcliment.controllers
{
	import com.controllers.SquidMetricsController;
	import com.dcliment.elements.MetricsElement;
	import com.events.NavigationEvent;
	
	import flash.utils.Dictionary;

	/**
	 * This is the XML i use
	 * <metrics doesRecord="true" dbPath="data\Novartis_jukebox_collected_data.db" DeviceCode="DEVICE_01" AppCode="NOVARTIS_JUKEBOX"/>
	 * 
	 * var _metricsModel:MetricsModel = new MetricsModel();
	 * _metricsModel.dbPath = "path to database";
	 * _metricsModel.appCode = "Application Code";
	 * _metricsModel.deviceCode = "Device Code"
	 * _metricsModel.doesRecord = Boolean;
	 * 
	 * 
	 * var _metricsController:MetricsController = MetricsController.getInstance();
	 * _metricsController.setModel(_metricsModel)
	 * _metricsController.init();
	 * _metricsController.endSession(); // FOR REASONS unknown to me you have to end the session before you start a session for it to record.
	 * _metricsController.startSession(); // start session (when activity starts);
	 * _metricsController.recordPage($token); // to record
	 * _metricsController.endSession(); // ends session (usually when attract loop fires). IF YOU DON"T END THE SESSION none of the information will be grabbed
	 * 
	 *  :::::::::: NOTES ::::::::::::::
	 * ** Must publish out as air 3.2 and above because if not its going to be missing the swlEngine.
	 * ** TO end session when application is closed to save the data run this code
	 *  > _appModel.theStage.nativeWindow.addEventListener(Event.CLOSING, onAppClose, false, 0, true);
	 *  > private function onAppClose(e:Event):void { _metricsController.endSession();}
	 * ** OVERLAYS :: i think overlays are handled as savePageToDB(overlayToken) and then when cover lay is closed call savePageToDB(original pages Token)
	 * 
	 * @author dcliment
	 * version 1.1;
	 * 
	 */
	public class MetricsController extends SquidMetricsController 
	{
		
		private static var __allowInstance:Boolean;
		private static var __instance:MetricsController;
		//
		private var _sessionRecording:Boolean = false;
		private var _lastPageToken:String;
		
		public function MetricsController()
		{
			super();
			
			if (!__allowInstance) {
				throw new Error("An instance of this class could not be created. Call MetricsController.getInstance() instead. ");
			}
		}
	
		public static function getInstance():MetricsController 
		{
			if (__instance == null) {
				__allowInstance = true;
				trace("MetricsController has been instantiated");
				__instance = new MetricsController();
				__allowInstance = false;
			}
			return __instance;
		}
		//-------------------------------------------
		// TRACK
		public function track(m:MetricsElement):void
		{
			if(m.type == MetricsElement.PAGE)
				this.recordPage(m.token);
			else if(m.type == MetricsElement.EVENT)
				this.recordObjectEvent(m.token, m.objectState);
			else if(m.type == MetricsElement.QUESTION)
				this.recordQuestionAnswer(m.token, m.hashTag, m.textAnswer);
			else
				throw new Error("MetricsController.track() ERROR :: m.type = " + m.type + " for m.token = " + m.token + " :: is not an appropriate type");
		}
		//------------------------------------
		// RECORD functions
		/**
		 * Record a new activated pages's TOKEN
		 *  
		 * @param $token :: Page Token
		 * 
		 */		
		public function recordPage($token:String):void
		{
			trace("MetricsController.recordPage() :: $token = " + $token);
			
			_lastPageToken = $token;
			
			this._RecordPageToken($token);
			
		}
		
		/**
		 * Record events that happen inside of the pages
		 * 
		 * ex: Video Play/Pause button
		 * _metricsController.recordObjectEvent("VideoPlayBtn","ON")
		 * _metricsController.recordObjectEvent("VideoPlayBtn","OFF")
		 * 
		 * @param $objectToken :: TOKEN
		 * @param $objectState :: STATE
		 * 
		 */		
		public function recordObjectEvent($objectToken:String, $objectState:String):void
		{
			trace("MetricsController.recordObjectEvent() :: $objectToken = " + $objectToken + " :: $objectState = " + $objectState);
			
			this._RecordPageEventToken($objectToken, $objectState);
		}
		/**
		 * RECORD question and answers
		 * 
		 * _metricsController.recordAnswerQuestion("QUESTION", "ANSWER NUMBER", "TEXT ANSWER");
		 *  
		 * @param $pageQuestionToken
		 * @param $hashCode :: (1,2,4,8) 2^2;
		 * @param $textValue
		 * 
		 */		
		public function recordQuestionAnswer($pageQuestionToken:String, $hashCode:Number, $textValue:String = null):void
		{
			if(_recordMetrics)
			{
				trace("MetricsController.recordQuestionAnswer() :: ppageToken = " + _lastPageToken + " :: pageQuestionToken = " + $pageQuestionToken + " :: $hasCode = " + $hashCode + " :: $textValue = " + $textValue);
				// savePageAnswer(pageToken:String, pageQuestionToken:String, hashCode:Number, textValue:String)
				_dbEngine.savePageAnswer(_lastPageToken,$pageQuestionToken,$hashCode,$textValue);
			}
		}
		//-----------------------------
		// OVERRIDES
		override public function startSession():void 
		{
			super.startSession();
			
			this.sessionRecording = true;
		}
		
		override public function endSession():void 
		{
			super.endSession();
			
			this.sessionRecording = false;
		}
		//-----------------------------
		// ACCESSORS
		public function get sessionRecording():Boolean
		{
			return _sessionRecording;
		}

		public function set sessionRecording(value:Boolean):void
		{
			_sessionRecording = value;
		}
	
	}
}